<template>
	<view>
		<van-dialog id="van-dialog" />
		<view>
			<block v-for="item in historyGoodsList" :key="item.goodsid">
				<shop-good :gid="item.goodsid" :goodsimg="item.goodsimg" :goodsname="item.goodsname" :goodsprice="item.goodsbaseprice" :mid="item.merchantsid" @longpress="delCurrentRecord(item.goodsid)"></shop-good>
			</block>
		</view>
	</view>
</template>

<script>
import Dialog from '../../wxcomponents/vant-weapp/dialog/dialog'
	export default {
		onLoad() {
			this.getBrowseHistoryGoods()
		},
		data() {
			return {
				// 存储本地存储中记录的商品id值列表
				historyGoodsIdList: [],
				// 存储用户的浏览历史记录
				historyGoodsList: []
			}
		},
		methods: {
			// 获取本地存储中记录的商品id值
			async getBrowseHistoryGoods() {
				this.historyGoodsIdList = JSON.parse(uni.getStorageSync('history'))
				if(this.historyGoodsIdList.length === 0) return
				const { data: res } = await uni.$http.get('/goods', { id: this.historyGoodsIdList.join(',') })
				if(res.meta.status !== 200) return Toast.fail('加载商品失败')
				this.historyGoodsList = res.recordset
				console.log(this.historyGoodsList)
			},
			delCurrentRecord(id) {
				Dialog.confirm({
				  title: '提示',
				  message: '确认删除该条历史记录?',
				})
				.then(() => {
					const index = this.historyGoodsIdList.findIndex(item => item == id)
					this.historyGoodsIdList.splice(index, 1)
					uni.setStorageSync('history', JSON.stringify(this.historyGoodsIdList))
					this.getBrowseHistoryGoods()
				})
				.catch(() => {
					console.log('取消删除')
				})
			}
		}
	}
</script>

<style>

</style>
